<?php
/*
 * @package WordPress
 * @subpackage ZeroWeight
 * 
 * Created by John Przeslakowski
 * http://blog.przeslakowski.com
 *
 */
?>
<?php

if(!isset($content_width)):
	$content_width = 960;
endif;

function sidebars_init()
{

	register_sidebar(array(
		'name' => 'Primary widget',
		'id' => 'primary-widget-area',
		'description' => 'The primary widget area',
		'before_title'  => '<h4 class="widgettitle">',
		'after_title'   => '</h4>'
	));

	register_sidebar(array(
		'name' => 'Secondary widget',
		'id' => 'secondary-widget-area',
		'description' => 'The secondary widget area',
		'before_title'  => '<h4 class="widgettitle">',
		'after_title'   => '</h4>'
	));

	register_sidebar(array(
		'name' => 'First Footer Widget Area',
		'id' => 'first-footer-widget-area',
		'description' => 'The first footer widget area',
		'before_title'  => '<h5 class="widgettitle">',
		'after_title'   => '</h5>'
	));

	register_sidebar(array(
		'name' => 'Second Footer Widget Area',
		'id' => 'second-footer-widget-area',
		'description' => 'The second footer widget area',
		'before_title'  => '<h5 class="widgettitle">',
		'after_title'   => '</h5>'
	));

	register_sidebar(array(
		'name' => 'Third Footer Widget Area',
		'id' => 'third-footer-widget-area',
		'description' => 'The third footer widget area',
		'before_title'  => '<h5 class="widgettitle">',
		'after_title'   => '</h5>'
	));

	register_sidebar(array(
		'name' => 'Fourth Footer Widget Area',
		'id' => 'fourth-footer-widget-area',
		'description' => 'The fourth footer widget area',
		'before_title'  => '<h5 class="widgettitle">',
		'after_title'   => '</h5>'
	));
}

function the_breadcrumb()
{
	?>
	<div id="zero-bread">
		<nav>
		<a href="<?php echo home_url(); ?>" title="<?php echo esc_attr(get_bloginfo('name', 'display')); ?>" rel="home"><?php bloginfo('name'); ?></a>
		<?php
		if(is_category() || is_single()):
			echo " &rarr; "; the_category(', ');				
			if(is_single()):
				if(is_attachment()):
					the_title();
				else:
					echo " &rarr; "; the_title();
				endif;
			endif;
		elseif(is_page()):			
			echo " &rarr; "; echo the_title();				
		endif;
		
		if(is_tag()):				
			echo " &rarr; "; single_tag_title();				
		elseif(is_day()):				
			echo " &rarr; Archive for "; the_time('F jS, Y');				
		elseif(is_month()):				
			echo " &rarr; Archive for "; the_time('F, Y');				
		elseif(is_year()):				
			echo " &rarr; Archive for "; the_time('Y');
		elseif(is_author()):
			echo " &rarr; Author Archive";
		elseif(isset($_GET['paged']) && !empty($_GET['paged'])):
			echo " &rarr; Blog Archives";
		elseif(is_search()):
			echo " &rarr; Search Results";
		elseif(is_404()):
			echo ' &rarr; The Dreaded 404 Error: File not found!';
		endif;
		?>
		</nav>
	</div>
	<?php
}

function zero_scripts()
{
	if (!is_admin()): 			
		//jQUery
		wp_enqueue_script("jquery");
		
		// Skeleton
		wp_register_style('zero-skeleton-base', get_stylesheet_directory_uri() . '/lib/skeleton/stylesheets/base.css', false, false, 'screen, handheld, projection, tv');
		wp_enqueue_style('zero-skeleton-base');
		
		wp_register_style('zero-skeleton-skeleton', get_stylesheet_directory_uri() . '/lib/skeleton/stylesheets/skeleton.css', false, false, 'screen, handheld, projection, tv');
		wp_enqueue_style('zero-skeleton-skeleton');
		
		wp_register_style('zero-skeleton-layout', get_stylesheet_directory_uri() . '/lib/skeleton/stylesheets/layout.css', false, false, 'screen, handheld, projection, tv');
		wp_enqueue_style('zero-skeleton-layout');		
		
		// Fonts
		wp_register_style('zero-fonts', get_stylesheet_directory_uri() . '/lib/fonts/stylesheet.css', false, false, 'screen, handheld, projection, tv');
		wp_enqueue_style('zero-fonts');
		
		// Fancybox
		wp_register_script('zero-fancybox', get_stylesheet_directory_uri() . '/lib/fancybox/jquery.fancybox-1.3.4.pack.js');
		wp_enqueue_script('zero-fancybox');
		wp_register_script('zero-fancybox-scroll', get_stylesheet_directory_uri() . '/lib/fancybox/jquery.mousewheel-3.0.4.pack.js');
		wp_enqueue_script('zero-fancybox-scroll');
		wp_register_style('zero-fancybox-css', get_stylesheet_directory_uri() . '/lib/fancybox/jquery.fancybox-1.3.4.css', false, false, 'screen, handheld, projection, tv');
		wp_enqueue_style('zero-fancybox-css');
		
		// Zero CSS
		wp_register_style('zero-css', get_stylesheet_directory_uri() . '/zero.css', false, false, 'screen, handheld, projection, tv');
		wp_enqueue_style('zero-css');
		
		// prCSSit
		wp_register_style('zero-prcssit', get_stylesheet_directory_uri() . '/lib/prcssit/style.css', false, false, 'print');
		wp_enqueue_style('zero-prcssit');
		
		// Color scheme
		$options = get_option('zero_options');
		
		switch($options['zero_color']):
			case 'gold':
			default:
				wp_register_style('zero-color-scheme', get_stylesheet_directory_uri() . '/zero-gold.css', false, false, 'screen, handheld, projection, tv');
				wp_enqueue_style('zero-color-scheme');
			break;
			
			case 'gray':
			default:
				wp_register_style('zero-color-scheme', get_stylesheet_directory_uri() . '/zero-gray.css', false, false, 'screen, handheld, projection, tv');
				wp_enqueue_style('zero-color-scheme');
			break;
			
			case 'green':
			default:
				wp_register_style('zero-color-scheme', get_stylesheet_directory_uri() . '/zero-green.css', false, false, 'screen, handheld, projection, tv');
				wp_enqueue_style('zero-color-scheme');
			break;
			
			case 'purple':
			default:
				wp_register_style('zero-color-scheme', get_stylesheet_directory_uri() . '/zero-purple.css', false, false, 'screen, handheld, projection, tv');
				wp_enqueue_style('zero-color-scheme');
			break;
			
			case 'red':
			default:
				wp_register_style('zero-color-scheme', get_stylesheet_directory_uri() . '/zero-red.css', false, false, 'screen, handheld, projection, tv');
				wp_enqueue_style('zero-color-scheme');
			break;
			
			case 'custom':
			default:
				wp_register_style('zero-color-scheme', get_stylesheet_directory_uri() . '/zero-custom.css', false, false, 'screen, handheld, projection, tv');
				wp_enqueue_style('zero-color-scheme');
			break;
			
			case 'blue':
			default:
				wp_register_style('zero-color-scheme', get_stylesheet_directory_uri() . '/zero-blue.css', false, false, 'screen, handheld, projection, tv');
				wp_enqueue_style('zero-color-scheme');
			break;
		endswitch;		
		
	endif;
}

function encode_code_in_comment($source)
{
	$encoded = preg_replace_callback('/<code>(.*?)<\/code>/ims', create_function('$matches', '$matches[1] = preg_replace(array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "", $matches[1]); return "<code>" . htmlentities($matches[1]) . "</code>";'), $source);
	
	if($encoded):
		return $encoded;
	else:
		return $source;
	endif;
}

function is_ie()
{
	if(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 7.0") !== false || strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 8.0") !== false || strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 6.0") !== false):
		return true;
	else:
		return false;
	endif;
}

function zero_sticky_bar()
{
	
	$options = get_option('zero_options');
	
	if(empty($options['zero_bar'])):
	
		if(!is_admin()): ?>
			<script type="text/javascript">
			<!--<![CDATA[-->
				var $=jQuery;var target="body";var menu_title="#zero-blogname";var extra_class="zero-menu-created";var source="#zero-nav ul";var new_menu="#zero-clone";$(window).scroll(function(){if(!$(target).hasClass(extra_class)){if($(document).scrollTop()>=$(source).offset().top){$(target).addClass(extra_class);if($(new_menu).length>0){$(new_menu).remove()}$(source).clone().prependTo(target).attr("id",new_menu.substring(1,new_menu.length)).hide();$(new_menu).prepend("<li>"+$(menu_title).text()+"</li>");$(new_menu).removeAttr("class");$(new_menu+" li").removeAttr("id").removeAttr("class");$(new_menu).fadeIn("fast")}}else{if($(document).scrollTop()<=$(source).offset().top){if($(new_menu).length>0){$(new_menu).fadeOut("fast")}$(target).removeClass(extra_class)}}});
			<!--]]>-->	
			</script>
		<?php endif;
	
	endif;
}

function zero_login_bar()
{	
	$options = get_option('zero_options');
	
	if(empty($options['zero_login'])): ?>
		<script type="text/javascript">
		<!--<![CDATA[-->
			var $=jQuery;var handle_link="#zero-handle-link";var login_form="#zero-login";$(document).ready(function(){$(login_form).hide()});$(handle_link).click(function(a){a.preventDefault();if($(handle_link).hasClass("open")){$(login_form).slideUp("fast");$(this).removeClass("open")}else{$(this).addClass("open");$(login_form).slideDown("fast")}});
		<!--]]>-->	
		</script>
	<?php endif;
}

function zero_login_enabled()
{
	$options = get_option('zero_options');
	
	if(!empty($options['zero_login'])){
		return false;
	}
	else{
		return true;
	}
}

function zero_options_page()
{
	add_theme_page(__('ZeroWeight settings'), __('ZeroWeight settings'), 'edit_themes', basename(__FILE__), 'zero_settings_page');
}
 
function zero_settings_page()
{
?>
	<div class="wrap">
		<div class="icon32" id="icon-themes"></div>
		
		<h2>ZeroWeight Settings</h2>
		
		<?php $path = get_template_directory_uri(); ?>
		
		<?php if(!empty($_GET['settings-updated']) && $_GET['settings-updated'] == 'true'): ?>
		
		<div id="setting-error-settings_updated" class="updated settings-error"><p><strong>Settings saved.</strong></p></div>
		
		<?php endif; ?>
		
		<table width="100%" border="0" cellspacing="10" cellpadding="0">
			<tr>
				<td valign="top">
					<form method="post" action="options.php" enctype="multipart/form-data">
						<?php settings_fields('zero_options'); ?>
						<?php do_settings_sections(__FILE__); ?>
						<p class="submit"><input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" /></p>
					</form>
				</td>
			</tr>
		</table>
	</div>
<?php
}
 
function zero_fields()
{ 
	register_setting('zero_options', 'zero_options');
 
	add_settings_section('main_section', 'General settings', 'zero_divider', __FILE__);
	add_settings_field('zero_bar', 'Disable dynamic bar:', 'zero_bar_setting', __FILE__, 'main_section');
	add_settings_field('zero_login', 'Disable dynamic login:', 'zero_login_setting', __FILE__, 'main_section');
	add_settings_field('zero_color', 'Color scheme:', 'zero_color_setting', __FILE__, 'main_section');
}
 


function zero_divider(){
	?>
	<div style="background: #fff; border-bottom: solid 1px #f6f6f6; height: 1px;"></div>
	<?php
}
 
function zero_bar_setting() {
	$options = get_option('zero_options');
	?>
	<input id="zero_nav_opt" type="checkbox" name="zero_options[zero_bar]" <?php if(!empty($options['zero_bar'])){ echo 'checked="checked" '; } ?>/> <label for="zero_nav_opt"><em>If selected, theme will <strong>not show</strong> the dynamic sticky menu bar</em></label>
	<?php
}

function zero_login_setting() {
	$options = get_option('zero_options');
	?>
	<input id="zero_login_opt" type="checkbox" name="zero_options[zero_login]" <?php if(!empty($options['zero_login'])){ echo 'checked="checked" '; } ?>/> <label for="zero_login_opt"><em>If selected, theme will <strong>not show</strong> the dynamic login menu</em></label>
	<?php
}

function zero_color_setting() {
	$options = get_option('zero_options');
	?>
	
	<select name="zero_options[zero_color]">
		<option value="blue" <?php if($options['zero_color'] == 'blue'){ echo ' selected="selected"'; } ?>>Blue (default)</option>
		<option value="gold" <?php if($options['zero_color'] == 'gold'){ echo ' selected="selected"'; } ?>>Gold</option>
		<option value="gray" <?php if($options['zero_color'] == 'gray'){ echo ' selected="selected"'; } ?>>Gray</option>
		<option value="green" <?php if($options['zero_color'] == 'green'){ echo ' selected="selected"'; } ?>>Green</option>
		<option value="purple" <?php if($options['zero_color'] == 'purple'){ echo ' selected="selected"'; } ?>>Purple</option>
		<option value="red" <?php if($options['zero_color'] == 'red'){ echo ' selected="selected"'; } ?>>Red</option>
		<option value="custom" <?php if($options['zero_color'] == 'custom'){ echo ' selected="selected"'; } ?>>Custom</option>
	</select>
	
	<?php
}

function zero_add_menu_link() {
	global $wp_admin_bar;
	$wp_admin_bar->add_menu(array(
		'parent' => 'appearance',
		'title' => 'ZeroWeight settings',
		'href' => admin_url('themes.php?page=functions.php'),
	));
}

function zero_fancybox()
{
	if(!is_admin()):
?>
	<script type="text/javascript">
	<!--<![CDATA[-->	
		jQuery(document).ready(function(){
			
			jQuery('.gallery-icon a').fancybox({
				
				'titlePosition'		:	'over',
				'overlayOpacity'	:	0.9,
				'overlayColor'		:	'#000'
				
			});
			
		});	
	<!--]]>-->	
	</script>
<?php
	endif;
}

function zero_add_rel($anchor_tag, $image_id)
{
    $image = get_post($image_id);
    $rel = '';
	
    if(isset($image->post_parent)):
        $rel = ' rel="attached-to-' . (int) $image->post_parent . '"';
    endif;
	
    if(!empty($rel)):
        $anchor_tag = str_replace('<a', '<a' . $rel, $anchor_tag);
	endif;
	
    return $anchor_tag;
}

remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'adjacent_posts_rel_link');
add_action('wp_before_admin_bar_render', 'zero_add_menu_link');
add_action('widgets_init', 'sidebars_init');
add_action('wp_enqueue_scripts', 'zero_scripts');
add_action('wp_footer', 'zero_sticky_bar');
add_action('wp_footer', 'zero_login_bar');
add_action('admin_init', 'zero_fields');
add_action('admin_menu', 'zero_options_page');
add_action('wp_print_footer_scripts', 'zero_fancybox');
add_filter('wp_get_attachment_link', 'zero_add_rel', 1, 2);
add_filter('pre_comment_content', 'encode_code_in_comment');
add_theme_support('post-formats', array('gallery'));
add_theme_support('automatic-feed-links');
add_theme_support('post-thumbnails');
set_post_thumbnail_size(160, 160, true);

?>